﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DienThoai
{
    /// <summary>
    /// Interaction logic for Graph.xaml
    /// </summary>
    public partial class Graph : Window
    {
        DienThoaiDataContext db = new DienThoaiDataContext();
        //Đồ thị
        private ChartStyleGridlines cs;
        private Legend lg = new Legend();
        private DataCollection dc = new DataCollection();
        private DataSeries ds = new DataSeries();
        int year;
        public Graph(int year)
        {
            InitializeComponent();
            this.year = year;
            AddChart();
            lg.LegendCanvas = legendCanvas;

            lg.IsLegend = true;

            lg.IsBorder = false;

            lg.AddLegend(cs, dc);
        }
        private void AddChart()
        {

            cs = new ChartStyleGridlines();
            //cs.YTick = maxAmount / 5;
            cs.ChartCanvas = chartCanvas;
            cs.TextCanvas = textCanvas;
            cs.Title = "Đồ thị doanh thu theo tháng";
            cs.Xmin = 1;
            cs.Xmax = 30;
            cs.Ymin = 0;
            cs.GridlinePattern = ChartStyleGridlines.GridlinePatternEnum.Dot;
            cs.GridlineColor = Brushes.Black;

            double maxMonthAmountPerYear = 10;
            var MonthAmounts = from j in db.Bills
                               where j.SellDate.Value.Year == year
                               select j;
            //Tháng bán nhiều tiền nhất.
            foreach (var MaxAmount in MonthAmounts)
            {
                if (MaxAmount.TotalPrice > maxMonthAmountPerYear)
                    maxMonthAmountPerYear = double.Parse(MaxAmount.TotalPrice.ToString());
            }
            cs.Ymax = maxMonthAmountPerYear;
            cs.YTick = maxMonthAmountPerYear / 5;
            cs.AddChartStyle(tbTitle, tbXLabel, tbYLabel);
            for (int m = 0; m < 12; m++)
            {
                switch (m)
                {
                    case 0:
                        ds = new DataSeries();
                        ds.LineColor = Brushes.Blue;
                        ds.SeriesName = "Tháng 1";
                        ds.LineThickness = 1; break;
                    case 1:
                        ds = new DataSeries();
                        ds.LineColor = Brushes.Brown;
                        ds.SeriesName = "Tháng 2";
                        ds.LineThickness = 1; break;
                    case 2:
                        ds = new DataSeries();
                        ds.LineColor = Brushes.CadetBlue;
                        ds.SeriesName = "Tháng 3";
                        ds.LineThickness = 2; break;
                    case 3:
                        ds = new DataSeries();
                        ds.LineColor = Brushes.Chocolate;
                        ds.SeriesName = "Tháng 4";
                        ds.LineThickness = 2; break;
                    case 4:
                        ds = new DataSeries();
                        ds.LineColor = Brushes.DarkBlue;
                        ds.SeriesName = "Tháng 5";
                        ds.LineThickness = 1; break;
                    case 5:
                        ds = new DataSeries();
                        ds.LineColor = Brushes.DarkCyan;
                        ds.SeriesName = "Tháng 6";
                        ds.LineThickness = 1; break;
                    case 6:
                        ds = new DataSeries();
                        ds.LineColor = Brushes.DarkOliveGreen;
                        ds.SeriesName = "Tháng 7";
                        ds.LineThickness = 2; break;
                    case 7:
                        ds = new DataSeries();
                        ds.LineColor = Brushes.DarkViolet;
                        ds.SeriesName = "Tháng 8";
                        ds.LineThickness = 1; break;
                    case 8:
                        ds = new DataSeries();
                        ds.LineColor = Brushes.DeepPink;
                        ds.SeriesName = "Tháng 9";
                        ds.LineThickness = 2; break;
                    case 9:
                        ds = new DataSeries();
                        ds.LineColor = Brushes.Gold;
                        ds.SeriesName = "Tháng 10";
                        ds.LineThickness = 1; break;
                    case 10:
                        ds = new DataSeries();
                        ds.LineColor = Brushes.GreenYellow;
                        ds.SeriesName = "Tháng 11";
                        ds.LineThickness = 1; break;
                    case 11:
                        ds = new DataSeries();
                        ds.LineColor = Brushes.Lavender;
                        ds.SeriesName = "Tháng 12";
                        ds.LineThickness = 1; break;

                }
                //thêm điểm (0,0) và đồ thị
                double x0 = 0;
                double y0 = 0;
                ds.LineSeries.Points.Add(new Point(x0, y0));
                dc.DataList.Add(ds);
                for (int d = 0; d < 31; d++)
                {

                    double totalAmountPerDay = 0;


                    var Month1 = from j in db.Bills
                                 where j.SellDate.Value.Year == year && j.SellDate.Value.Month == m + 1 && j.SellDate.Value.Day == d + 1
                                 select j;
                    //Số tiền trong ngày
                    foreach (var Amount in Month1)
                    {
                        if (Amount.TotalPrice != null)
                            totalAmountPerDay += double.Parse(Amount.TotalPrice.ToString());
                    }
                    double x1 = d;
                    double y1 = totalAmountPerDay;
                    ds.LineSeries.Points.Add(new Point(x1, y1));
                }
            }
            dc.AddLines(chartCanvas, cs);
        }
    }
}
